package com.wlcorp.forcazamis.dao.stock.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.stock.IStockFittingDAO;
import com.wlcorp.forcazamis.domain.stock.StockFitting;

public class StockFittingDAOImpl extends HibernateDaoSupport implements IStockFittingDAO {

	public void deleteStockFitting(StockFitting stockFitting)
			throws DaoException {
		stockFitting.setDeleteFlag(true);
		getHibernateTemplate().update(stockFitting);
	}

	public StockFitting getStockFittingById(Long stockFittingId)
			throws DaoException {
		return (StockFitting)getHibernateTemplate().get(StockFitting.class,stockFittingId);
	}

	@SuppressWarnings("unchecked")
	public List getStockFittingList(final StockFitting stockFitting)
			throws DaoException {
		final StringBuffer queryString = new StringBuffer("select stockFitting from StockFitting as stockFitting where 1=1 and stockFitting.deleteFlag=false");
		 
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != stockFitting && null != stockFitting.getMinRownum()){
					query.setFirstResult(stockFitting.getMinRownum());
					query.setMaxResults(stockFitting.getNumPerPage());
				}
				return query.list();
			}
		});
	}

	public void saveStockFitting(StockFitting stockFitting) throws DaoException {
		getHibernateTemplate().flush();
		getHibernateTemplate().clear();
		getHibernateTemplate().saveOrUpdate(stockFitting);
	}

	 

}
